package com.youfang.sixthmedia;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TabHost;

import com.gzit.utils.AndroidUtils;
import com.youfang.sixthmedia.ui.DiscountBuildActivity;
import com.youfang.sixthmedia.ui.GrouponBuildActivity;
import com.youfang.sixthmedia.ui.NewBuildActivity;
import com.youfang.sixthmedia.ui.center.CFavActivity;
import com.youfang.sixthmedia.ui.searcher.SearcherBuildingActivity;

/**
 * 页签
 * 
 * @author teddy
 * 
 */
public class TabMainActivity extends TabActivity {

	private TabHost mHost;
	private Intent newIntent;
	private Intent searcherIntent;
	private Intent discountIntent;
	private Intent tuangouIntent;
	private Intent favoriteIntent;

	private int[] rids = new int[] { R.id.mainRB, R.id.searcherRB,
			R.id.discountRB, R.id.tuangouRB, R.id.favoriteRB };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		AndroidUtils.Screen.noWindowTitle(this);
		this.setContentView(R.layout.tab_main_activity);

		// 设置初始的选中状态
		setupTabs();
		initRadioBtn();

		Intent intent = this.getIntent();
		int index = intent.getIntExtra("tabIndex", 0);

		((RadioButton) this.findViewById(rids[index])).setChecked(true);

	}

	private OnCheckedChangeListener switchTabListener = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {

			if (isChecked) {
				int rid = buttonView.getId();

				int[] ids = rids;
				for (int i = 0; i < ids.length; i++) {
					if (rid == ids[i]) {// 找到对应的被按中的radio
						TabMainActivity.this.mHost
								.setCurrentTabByTag("tab" + i);
						break;
					}
				}

			}
		}

	};

	private void initRadioBtn() {
		for (int id : rids) {
			((RadioButton) findViewById(id))
					.setOnCheckedChangeListener(switchTabListener);

		}

	}

	private void setupTabs() {

		this.newIntent = new Intent(this, NewBuildActivity.class);
		this.searcherIntent = new Intent(this, SearcherBuildingActivity.class);
		this.discountIntent = new Intent(this, DiscountBuildActivity.class);
		this.tuangouIntent = new Intent(this, GrouponBuildActivity.class);
		this.favoriteIntent = new Intent(this, CFavActivity.class);
		this.mHost = getTabHost();
		this.mHost.addTab(buildTabSpec("tab0", "tabitem",
				R.drawable.ic_launcher, this.newIntent));

		this.mHost.addTab(buildTabSpec("tab1", "tabitem",
				R.drawable.ic_launcher, this.searcherIntent));

		this.mHost.addTab(buildTabSpec("tab2", "tabitem",
				R.drawable.ic_launcher, this.discountIntent));

		this.mHost.addTab(buildTabSpec("tab3", "tabitem",
				R.drawable.ic_launcher, this.tuangouIntent));

		this.mHost.addTab(buildTabSpec("tab4", "tabitem",
				R.drawable.ic_launcher, this.favoriteIntent));

	}

	private TabHost.TabSpec buildTabSpec(String tag, String resLabel,
			int resIcon, final Intent content) {
		return this.mHost.newTabSpec(tag)
				.setIndicator(resLabel, getResources().getDrawable(resIcon))
				.setContent(content);
	}
	
	
	
	
	
	

}
